package org.hxyjs.controller;

import org.hxyjs.dto.DTO;
import org.hxyjs.entity.Books;
import org.hxyjs.entity.Books;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.BooksService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@Controller
@RequestMapping("/booksController")
public class BooksController {
	@Resource
	BooksService booksService;
	@RequestMapping("/getAllBooks")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
    public DTO<Books> getAllBooks() throws Exception {
    	System.out.println("查询全部书籍");
		return booksService.getAllBookss();
    }

	@RequestMapping("/deleteBooksByBno/{bno}")
	@ResponseBody
    public DTO<Books> deleteBooksByBno(@PathVariable("bno") String bno) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
    	System.out.println(bno+"删除书籍");
    	return booksService.deleteBooksByBno(bno);
    }

	@RequestMapping("/updateBooksByBno")
	@ResponseBody
	public DTO<Books> updateBooksByBno(@RequestBody Books books) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改书籍");
		return booksService.updateBooksByBno(books);
	}


	@RequestMapping("/addBooks")
	@ResponseBody
	public DTO<Books> addBooks(@RequestBody Books books) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加书籍");
		System.out.println(books);
 		return booksService.addBooks(books);
	}
	@RequestMapping("/getBooksByPage")
	@ResponseBody
	public DTO<Books> getBooksByPage(PageInfo pageInfo) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("分页书籍");
		String cacheId = "BooksByPage" + pageInfo.getCurrPage();//自编redis每页数据缓存的key
		System.out.println(cacheId);
		return pageInfo.getCurrPage() > 2 ? booksService.getBooksByPage(pageInfo) :
				booksService.getBooksByPage(pageInfo, cacheId);
	}

}